# -*- coding: utf8 -*-
'''
# =======
# author = luwei
# email = luw2007@gmail.com
# =======
ubuntu 11.04 install tesseract-ocr3
sudo add-apt-repository ppa:alex-p/notesalexp
sudo apt-get update
sudo apt-get install tesseract-ocr
 
'''

# Imports
# =======
import  zlib
import base64
import re
import os



#'''download img ,get the text from img'''
def get_img(id = 'IDC-V-12409',monitor_host = 'hn4.monitor.ceopen.cn'):
	path='/tmp/'
	name="viewer"
	password=""
	un='-F|-E|--height=100|--lower-limit=0|--rigid|--vertical-label=bits/sec|HRULE:75000000.0#800000:ifInOctets, ifOutOctets > 75.00M\\j|DEF:ifInOctets-raw=/opt/zenoss/perf/Devices/IDC-V-12376/os/interfaces/eth0/ifInOctets_ifInOctets.rrd:ds0:AVERAGE|CDEF:ifInOctets-rpn=ifInOctets-raw,8,*|CDEF:ifInOctets=ifInOctets-rpn|AREA:ifInOctets-rpn#00cc00ff:Inbound       |GPRINT:ifInOctets-rpn:LAST:cur\\:%5.2lf%s|GPRINT:ifInOctets-rpn:AVERAGE:avg\\:%5.2lf%s|GPRINT:ifInOctets-rpn:MAX:max\\:%5.2lf%s\\j|DEF:ifOutOctets-raw=/opt/zenoss/perf/Devices/IDC-V-12376/os/interfaces/eth0/ifOutOctets_ifOutOctets.rrd:ds0:AVERAGE|CDEF:ifOutOctets-rpn=ifOutOctets-raw,8,*|CDEF:ifOutOctets=ifOutOctets-rpn|LINE1:ifOutOctets-rpn#0000ff99:Outbound      |GPRINT:ifOutOctets-rpn:LAST:cur\\:%5.2lf%s|GPRINT:ifOutOctets-rpn:AVERAGE:avg\\:%5.2lf%s|GPRINT:ifOutOctets-rpn:MAX:max\\:%5.2lf%s\\j'	
	un2=re.sub('IDC-V-12376',id,un)
	un3 = re.sub('--height=100','--height=100',un2)
	un4 = base64.urlsafe_b64encode(zlib.compress(un3))
	url = "http://"+name+":"+password+"@"+monitor_host+":8080/zport/RenderServer/render?gopts=" + un4 +"&amp;drange=129600&amp;width=400"
	cmd1 = 'curl -s "'+url+'" -o '+path+id+'.png'
	os.system(cmd1)
	cmd2= 'convert -crop 330x30+160+170 -scale 205%  -colorspace Gray '+path+id+'.png '+path+id+'.png'
	os.system(cmd2)
	cmd3='tesseract '+path+id+'.png /tmp/output 2>/dev/null'
	os.system(cmd3)
	output = open('/tmp/output.txt').read().replace('|n','M').replace('B','8').replace('?','7').replace('‘3','9').replace('l','1').replace('EJ','0').replace('\'I','7').replace('\'3','9').replace('S','5').replace('\\r','v').replace('0k','k').replace('0M','M').replace('Ok','k')
	print id, len(output.split()),"\r\n", #output.split('\n')[1].replace(' ','|').replace(':|',':').replace('|:',':').split('|')
	#print  output
	if 'eth0' not in output:	

		dict_l ={}
		try:
			for i in output.split('\n')[1].replace(' ','|').replace(':|',':').replace('|:',':').split('|'):
				if i.split(':')[1][-1] == 'k':
					dict_l[i.split(':')[0].lower()]=float(i.split(':')[1][:-1])
				elif  i.split(':')[1][-1] == 'M':
					dict_l[i.split(':')[0].lower()]=float(i.split(':')[1][:-1]) * 1024
				else:
					dict_l[i.split(':')[0].lower()]=i.split(':')[1]
		except:
			print "error"
		print "avg(k):",dict_l.get('avg'),"\t,max(k):",dict_l.get('max')
	else:
		print "error !"*3# ,output
def main():
	print "done"

if __name__ == '__main__':
	for i in xrange(10):
		out_reader.get_img('IDC-V-1235'+str(i))
	main()
